package com.hp.his.basic.web;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.hp.his.basic.entity.Choice;
import com.hp.his.basic.entity.ExamDetail;
import com.hp.his.basic.entity.Question;
import com.hp.his.basic.service.UPMSBaseManager;
import com.hp.his.core.util.web.struts2.CrudActionSupport;

@Namespace("/exam")
@Results({    
       @Result(name = CrudActionSupport.RELOAD, location = "question.action", type = "redirect"),
})
public class QuestionAction extends  CrudActionSupport<Question>{
	
	private Integer questionId;	
	private Question question;
	private Integer examId;
		
	private String content_error;
	private String correctAnswer_error;
	
	private String[] choices = new String[10];	
	//默认为新建问题的情况，前面四个答案显示
	private String[] divVisiableFlags = {"block","block","block","block","none","none","none","none","none","none"};	
	private String[] answerFlags = new String[10];
	
	private String choic1;
	private String choic2;
	private String choic3;
	private String choic4;
	private String choic5;
	private String choic6;
	private String choic7;
	private String choic8;
	private String choic9;
	private String choic10;
	
	@Autowired
	private UPMSBaseManager baseManager;	

	@Override
	public String list() throws Exception {
		if(questionId==null){
			question = new Question();
		}else{
			question = baseManager.getQuestionById(questionId);		
			for(int i=0; i<question.getChoices().size(); i++){
				Choice choice = question.getChoices().get(i);
				String choiceKey = String.valueOf(choice.getChoiceKey());
				choices[i]=choice.getChoiceContent();
				answerFlags[i]= question.getCorrectAnswer().indexOf(choiceKey)!=-1 ? "checked":"";
				divVisiableFlags[i]="block";
			}
			
			choic1=choices[0];
			choic2=choices[1];
			choic3=choices[2];
			choic4=choices[3];
			choic5=choices[4];
			choic6=choices[5];
			choic7=choices[6];
			choic8=choices[7];
			choic9=choices[8];
			choic10=choices[9];
		}
		
		return SUCCESS;
	}
	
	public boolean validateFrom() {
		boolean flag = true;
		if (question.getContent() == null || question.getContent().length()<1){
			content_error = "Please fill Question Content.";
			flag =  false;
		}
		if (question.getCorrectAnswer() == null || question.getCorrectAnswer().length()<1){
			correctAnswer_error = "Please Choose at least one correct answer.";
			flag = false;
		}
		
		return flag;
	}
	
	public String add() throws Exception{		
		if(!validateFrom()){
			return 	SUCCESS;
		}
				
		ExamDetail exam = baseManager.getExamById(examId);
		question.setExam(exam);
		question.setChoices( new ArrayList<Choice>());
		if(question.getCorrectAnswer().contains(",")){
			question.setQuestionType(2);
		}else{
			question.setQuestionType(1);
		}
		
		choices[0] = choic1;
		choices[1] = choic2;
		choices[2] = choic3;
		choices[3] = choic4;
		choices[4] = choic5;
		choices[5] = choic6;
		choices[6] = choic7;
		choices[7] = choic8;
		choices[8] = choic9;
		choices[9] = choic10;
		
		for(int i=0; i<choices.length; i++){
			if(choices[i]!=null && choices[i].length()>0){
				Choice ch = new Choice();
				ch.setChoiceContent(choices[i]);
				ch.setChoiceKey(i+1);
				ch.setQuestion(question);
				question.getChoices().add(ch);
			}
		}
		
		exam.getQuestions().add(question);
		baseManager.saveExam(exam);
		
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setCharacterEncoding("UTF-8");      
		response.setContentType("text/plain");  
		PrintWriter out = response.getWriter();  
		StringBuilder jason = new StringBuilder("{'status':1,'newQuestionHtml':'");
		
		jason.append( "<div style=\"width:1000px;float:left;padding:5px 5px 5px 5px;border:1px;border-color:#f5f5f5;border-style:solid;\" id=\"question_" +question.getQuestionId()+ "\"> " );
		jason.append("<b>" + exam.getQuestions().size() + ".</b>" + question.getContent());
		if(question.getQuestionType()==1){
			jason.append("(Singel Select)");
		}else{
			jason.append("(Mutliple Select)");
		}
		jason.append("<br/>");
		for(int i=0; i<question.getChoices().size(); i++){
			jason.append("&nbsp;&nbsp;&nbsp;&nbsp;" + i + ".&nbsp;" + question.getChoices().get(i).getChoiceContent()+"<br/>");
		}
		jason.append("</div>");		
		
		jason.append("<div style=\"float:left;position: relative; text-align: center; top: 5px; width:50px;height:50px;\" id=\"maintainQues_" + question.getQuestionId() + "\">"); 
		jason.append("<a href=\"#\" onclick=\"updateQuestion(" + question.getQuestionId() + 
				");\">编辑</a><br/><a href=\"#\" onclick=\"deleteQuestion(" + question.getQuestionId() + 
				");\">删除</a>");	
		jason.append("</div>'}");
		
		JSONObject jsonObject = JSONObject.fromObject(jason.toString());
		out.write(jsonObject.toString());

		out.flush();
		out.close();
		
		return 	null;
	}
	
	@Override
	public String delete() throws Exception {
		question =  baseManager.getQuestionById(questionId);	
		question.getExam().getQuestions().remove(question);
		baseManager.deleteQuestion(question);
		return null;
	}
		
	public String update() throws Exception{		
		if(!validateFrom()){
			return 	SUCCESS;
		}		

		String content = question.getContent();
		String correctAnswer = question.getCorrectAnswer();
		question =  baseManager.getQuestionById(questionId);

		if(correctAnswer.contains(",")){
			question.setQuestionType(2);
		}else{
			question.setQuestionType(1);
		}
		question.setContent(content);
		question.setCorrectAnswer(correctAnswer);
		
		choices[0] = choic1;
		choices[1] = choic2;
		choices[2] = choic3;
		choices[3] = choic4;
		choices[4] = choic5;
		choices[5] = choic6;
		choices[6] = choic7;
		choices[7] = choic8;
		choices[8] = choic9;
		choices[9] = choic10;
		
		for(int i=0; i<choices.length; i++){
			if(choices[i]!=null && choices[i].length()>0){
				question.getChoices().get(i).setChoiceContent(choices[i]);
			}
		}
		
		baseManager.saveQuestion(question);
		
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setCharacterEncoding("UTF-8");      
		response.setContentType("text/plain");  
		PrintWriter out = response.getWriter();  
		StringBuilder jason = new StringBuilder("{'status':2,'questionId':" +question.getQuestionId()+",'newQuestionHtml':'");
		
		jason.append("<b>" + question.getExam().getQuestions().size() + ".</b>" + question.getContent());
		if(question.getQuestionType()==1){
			jason.append("(Singel Select)");
		}else{
			jason.append("(Mutliple Select)");
		}
		jason.append("<br/>");
		for(int i=0; i<question.getChoices().size(); i++){
			jason.append("&nbsp;&nbsp;&nbsp;&nbsp;" + (i+1) + ".&nbsp;" + question.getChoices().get(i).getChoiceContent()+"<br/>");
		}
		jason.append("'}");
		
		JSONObject jsonObject = JSONObject.fromObject(jason.toString());
		out.write(jsonObject.toString());

		out.flush();
		out.close();
		
		return 	null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	@Override
	public Question getModel() {
		// TODO Auto-generated method stub
		return question;
	}

	public Integer getQuestionId() {
		return questionId;
	}

	public void setQuestionId(Integer questionId) {
		this.questionId = questionId;
	}

	public Integer getExamId() {
		return examId;
	}

	public void setExamId(Integer examId) {
		this.examId = examId;
	}

	public String getContent_error() {
		return content_error;
	}

	public void setContent_error(String content_error) {
		this.content_error = content_error;
	}

	public String getCorrectAnswer_error() {
		return correctAnswer_error;
	}

	public void setCorrectAnswer_error(String correctAnswer_error) {
		this.correctAnswer_error = correctAnswer_error;
	}

	public String[] getChoices() {
		return choices;
	}

	public void setChoices(String[] choices) {
		this.choices = choices;
	}

	public String[] getDivVisiableFlags() {
		return divVisiableFlags;
	}

	public void setDivVisiableFlags(String[] divVisiableFlags) {
		this.divVisiableFlags = divVisiableFlags;
	}

	public String[] getAnswerFlags() {
		return answerFlags;
	}

	public void setAnswerFlags(String[] answerFlags) {
		this.answerFlags = answerFlags;
	}

	public String getChoic1() {
		return choic1;
	}

	public void setChoic1(String choic1) {
		this.choic1 = choic1;
	}

	public String getChoic2() {
		return choic2;
	}

	public void setChoic2(String choic2) {
		this.choic2 = choic2;
	}

	public String getChoic3() {
		return choic3;
	}

	public void setChoic3(String choic3) {
		this.choic3 = choic3;
	}

	public String getChoic4() {
		return choic4;
	}

	public void setChoic4(String choic4) {
		this.choic4 = choic4;
	}

	public String getChoic5() {
		return choic5;
	}

	public void setChoic5(String choic5) {
		this.choic5 = choic5;
	}

	public String getChoic6() {
		return choic6;
	}

	public void setChoic6(String choic6) {
		this.choic6 = choic6;
	}

	public String getChoic7() {
		return choic7;
	}

	public void setChoic7(String choic7) {
		this.choic7 = choic7;
	}

	public String getChoic8() {
		return choic8;
	}

	public void setChoic8(String choic8) {
		this.choic8 = choic8;
	}

	public String getChoic9() {
		return choic9;
	}

	public void setChoic9(String choic9) {
		this.choic9 = choic9;
	}

	public String getChoic10() {
		return choic10;
	}

	public void setChoic10(String choic10) {
		this.choic10 = choic10;
	}


}
